/**
 * 
 */
package com.FrameWork.Synchoronization;

import java.util.Observable;
import java.util.Observer;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;

import com.umeng.analytics.MobclickAgent;



/**
 * @author steven.jiang
 *  同步监听程序
 */
public class SyncMonitor extends Service
implements Observer
{
	//私有成员
	private Context context = null;
	//实际同步对象
	private Synchronization _syncchronization = null;

	private SyncThread thread = null;
	//同步配置对象
	private SynchConfig _config = null;

	/// <summary>
	/// 负责同步前后处理的对象
	/// </summary>
	public ISyncProcess Process;

	/// <summary>
	/// 同步时间间隔,默认是30分钟,单位是分钟
	/// </summary>
	public int AutoSyncTimeSpan;


	public Observer Observer;
	/// <summary>
	/// 构造函数
	/// </summary>
	public SyncMonitor(Context context)
	{
		this.context =context;
		AutoSyncTimeSpan = 30; //自动同步时间间隔，默认设置为30分钟。
		_syncchronization = new Synchronization(context);
		_syncchronization.addObserver(this);

	}



	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		if(data == null)
		{
			
			if (!_syncchronization.SynchSuccess)
			{
				Toast toast = new Toast(this.context);
				toast.setDuration(5000);
				toast.setText("同步失败");
				toast.show();

			}
			else
			{
				if (Process != null)
				{
					Process.AfterSync();
					//new SyncLog().InsertSyncLog(1);
				}
				//记录同步日志
				Toast toast = new Toast(this.context);
				toast.setDuration(5000);
				toast.setText("同步成功");
				toast.show();
			}




		}
		else
		{
			if(Observer!= null)
			{
				Observer.update(observable, data);
			}
		}
	}




	/// <summary>
	/// 自动循环
	/// </summary>
	public void run() {
		// TODO Auto-generated method stub
		boolean NeedSync = false;
		//是否需要及时同步
		if (Process != null && Process.getHaveSync())
		{
			NeedSync = true;
		}
		//如果不需要进行同步，则马上返回
		if (!NeedSync)
		{
			return;
		}



		if (thread == null)
		{

			//获取最新同步配置
			_config = SynchConfig.Instance(context);
			//如果同步失败则进行第二次同步
			thread = new SyncThread();
			thread.run();

		}
	}


	private class SyncThread extends Thread
	{
		//该方法可能会考虑采用多线程的方式进行同步
		public void run()
		{
			try
			{
				//判断是否设置同步处理对象及处理同步前事件
				if (Process != null)
				{
					//同步之前进行预处理
					Process.BeforeSync();
					Thread.sleep(500);
				}
				_config.setSyncModel(SyncModel.UploadDownload);
				_syncchronization.SynchConfig = _config;
				_syncchronization.addObserver(SyncMonitor.this);
				if(_syncchronization.StartSynchronization(context))
				{
					Process.setHaveSync(false);
				}
				
			}
			catch(Exception err)
			{
				//记录错误信息
				err.printStackTrace();
				MobclickAgent.reportError(null, err.getMessage());
				
			}
			finally
			{
				_syncchronization.deleteObserver(SyncMonitor.this);
			}
		}
	}


	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}

}
